package com.nullpointer.xchatfinal.fragment;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.nullpointer.xchatfinal.R;
import com.nullpointer.xchatfinal.object.User;
import com.nullpointer.xchatfinal.object.UserInfo;
import com.nullpointer.xchatfinal.service.ChatService;
import com.nullpointer.xchatfinal.utils.DataManager;
import com.nullpointer.xchatfinal.utils.Utils;

public class SayHiDialogFragment extends DialogFragment {
	private EditText etStatus;
	private Button btPost;
	private Button btCancel;
	private OnSayHiDialogFragment listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_sayhi, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		// etStatus.setText(DataManager.getInstance(getActivity())
		// .getCurrentUser().getUserInfo().getStatus());
	}

	private void setEvent() {
		btPost.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.hideKeyboard(getActivity(), etStatus);
				updateProfile();
			}
		});
		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (listener != null) {
					listener.onCancel();
				}
				dismiss();
			}
		});

		etStatus.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (Utils.checkBadword(etStatus.getText().toString())) {
					btPost.setText("Don't use sexual content!");
					btPost.setEnabled(false);
					btCancel.setVisibility(View.GONE);
				} else {
					btPost.setText("Post");
					btPost.setEnabled(true);
					btCancel.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	protected void updateProfile() {
		String status = etStatus.getText().toString();
		User user = DataManager.getInstance(getActivity()).getCurrentUser();
		UserInfo userInfo = user.getUserInfo();
		if (status.trim().length() == 0) {
			status = userInfo.getStatus();
		}
		String oldStatus = userInfo.getStatus();
		if (!status.equals(oldStatus)) {
			userInfo.setStatus(status);
			user.setUserInfo(userInfo);
			DataManager.getInstance(getActivity()).setCurrentUser(user);
			boolean isInvisible = DataManager.getInstance(getActivity())
					.getInvisibleSetting();
			ChatService.getInstance().updateProfile(true, isInvisible);
			dismiss();
		} else {
			if (listener != null) {
				listener.onCancel();
			}
			dismiss();
		}

	}

	private void findViewIds(View view) {
		etStatus = (EditText) view.findViewById(R.id.et_status);
		btPost = (Button) view.findViewById(R.id.bt_post);
		btCancel = (Button) view.findViewById(R.id.bt_cancel);
	}

	public void setOnSayHiDialogFragment(OnSayHiDialogFragment listener) {
		this.listener = listener;
	}

	public interface OnSayHiDialogFragment {
		void onCancel();
	}
}
